package com.mdc.mansion.api.web.modules.dishes;

import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesCategoryService;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesCategoryDTO;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesCategory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/dishesCategory")
public class RestaurantDishesCategoryController extends BaseController {

	@Autowired
	private RestaurantDishesCategoryService restaurantDishesCategoryService;

	@ModelAttribute("restaurantDishesCategoryDTO")
	public RestaurantDishesCategoryDTO getRestaurantDTO(@RequestParam(required = false) String data) {
		if(StringUtils.isBlank(data)){return null;}
		try{
			RestaurantDishesCategoryDTO restaurantDishesCategoryDTO = JSONObject.parseObject(data,RestaurantDishesCategoryDTO.class);
			return restaurantDishesCategoryDTO;
		}catch (Exception e){}
		return null;
	}

	@ModelAttribute("restaurantDishesCategoryDTOList")
	public List<RestaurantDishesCategoryDTO> getRestaurantDTOList(@RequestParam(required = false) String data) {
		if(StringUtils.isBlank(data)){return null;}
		try{
			JSONObject jsonObject = JSONObject.parseObject(data);
			List<RestaurantDishesCategoryDTO> restaurantDishesCategoryDTOList = JSONObject.parseArray(jsonObject.getString("dishesCategoryList"),RestaurantDishesCategoryDTO.class);
			return restaurantDishesCategoryDTOList;
		}catch (Exception e){}
		return null;
	}

	@RequestMapping("list")
	public Result list(){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		List<RestaurantDishesCategoryDTO> restaurantDishesCategoryDTOs=restaurantDishesCategoryService.currentUserRestaurantDishesCategory();
		result.setProperty("dishesCategoryList",restaurantDishesCategoryDTOs);
		return result;
	}

	@RequestMapping("save")
	public Result save(@ModelAttribute("restaurantDishesCategoryDTO") RestaurantDishesCategoryDTO restaurantDishesCategoryDTO){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantDishesCategoryService.saveCurrentUserRestaurantDishesCategory(restaurantDishesCategoryDTO);
		return result;
	}

	@RequestMapping("delete")
	public Result delete(@ModelAttribute("restaurantDishesCategoryDTO") RestaurantDishesCategoryDTO restaurantDishesCategoryDTO){ //需要考虑是否是删除当前餐厅的品类
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		RestaurantDishesCategory restaurantDishesCategory=new RestaurantDishesCategory();
		BeanUtils.copyProperties(restaurantDishesCategoryDTO,restaurantDishesCategory);
		restaurantDishesCategoryService.deleteById(restaurantDishesCategory.getId());
		return result;
	}

	@RequestMapping("sort")
	public Result sort(@ModelAttribute("restaurantDishesCategoryDTOList") List<RestaurantDishesCategoryDTO> restaurantDishesCategoryDTOList){
		Result result=new Result(ResultCode.COMMON_SUCCESS);
		restaurantDishesCategoryService.sort(restaurantDishesCategoryDTOList);
		return result;
	}
}
